var limsKnowledgelibrary = Gikam.getPageObject({

    getEditListColumns: function (grid) {
        return [{
            checkbox: true
        }, {index: true},{
            field: 'fileType',
            title: 'T_LIMS_KNOWLEDGELIBRARY.FILETYPE',
            type: 'select',
            category: 'sys',
            param: {
                codeCategoryId: 'knowledgelibrary-fileType'
            },
           width: 160,
            validators : ['notEmpty']
        }, {
            field: 'fileName',
            title: 'T_LIMS_KNOWLEDGELIBRARY.FILENAME',
            width: 320,
        }, {
            field: 'fileSize',
            title: 'T_LIMS_KNOWLEDGELIBRARY.FILESIZE',
           width: 100,
        }, {
            field: 'option',
            title: 'T_LIMS_REPORT_TEMPLATE.OPTION',
            contentAlign: 'center',
            width: 200,
            formatter: function (index, value, row) {
                let fileFlag = Gikam.isNotEmpty(row.fileId);
                return Gikam.printf('<a href="javascript:;" onclick="limsKnowledgelibrary.uploadReportFile(\'{id}\', \'{index}\', \'{dbTable}\',\'{gridName}\')">{name}</a>&nbsp;&nbsp;<a href="javascript:;" onclick="limsKnowledgelibrary.downloadFile(\'{rowId}\')">{download}</a>', {
                    id: row.id,
                    index: index,
                    dbTable: 'T_LIMS_KNOWLEDGELIBRARY',
                    gridName: grid,
                    rowId: row.id,
                    name: fileFlag ? Gikam.propI18N('LIMS.MODULE.REPORTTEMPLATES.BUTTON.UPDATE') : Gikam.propI18N('LIMS.MODULE.REPORTTEMPLATES.BUTTON.UPLOADER'),
                    download: Gikam.propI18N('LIMS.BASEMODULE.BUTTON.FILE_DOWNLOAD'),
                });
            },
        }, {
            field: 'authorizedPeople',
            title: 'T_LIMS_KNOWLEDGELIBRARY.AUTHORIZEDPEOPLE',
            width: 360,
            validators : ['notEmpty']
        }, {
            field: 'activeStatus',
            title: 'T_LIMS_KNOWLEDGELIBRARY.ACTIVESTATUS',
            type: 'select',
            category: 'sys',
            param: {
                codeCategoryId: 'activatedFlag'
            },
            width: 80,
        }];
    },

    downloadFile: function (id) {
        let url = Gikam.printf(limsKnowledgelibrary.baseUrl + '/{rowId}/core-file', {
            rowId: id
        });
        Gikam.postSync(url, null).done(function (data) {
            if (Gikam.isNotEmpty(data)) {
                Gikam.download(data.downloadUrl);
            }
        });
    },

    uploadReportFile: function (id, index, dbTable, gridName) {
        let _this = this;
        Gikam.create('simpleUploader', {
            dbTable: dbTable,
            bizId: id,
            fill: true,
            bizCategory: 'audit',
            multiple: false,
            onBeforeFileUpload: function (file) {
                return true
            },
            onUploadSuccess: function (data) {
                Gikam.getComp(gridName).setData({
                    id: id,
                    index: index,
                    fileId: data[0].id,
                    fileName: data[0].name,
                    fileSize: data[0].size + 'KB'
                }, true);
            }
        });
    },

    getBaseInfoFormFields: function () {
        return [{
            field: 'fileType',
            title: 'T_LIMS_KNOWLEDGELIBRARY.FILETYPE',
            validators: ['strLength[0,16]', 'notEmpty'],
            type: 'select',
            category: 'sys',
            param: {
                codeCategoryId: 'knowledgelibrary-fileType'
            },
            firstBlank: false
        },{
            field: 'authorizedPeople',
            title: 'T_LIMS_KNOWLEDGELIBRARY.AUTHORIZEDPEOPLE',
            validators: ['strLength[0,100]', 'notEmpty'],
            type: 'choose',
            category: 'common-user-choose',
            cleanable: false,
            onAfterChoose: function (rows, index) {
                if (rows) {
                    let authorizedPeople = '';
                    let authorizedPeopleIds = '';
                    rows.forEach(function (row) {
                        authorizedPeople = authorizedPeople + row.userName + ',';
                        authorizedPeopleIds = authorizedPeopleIds + row.id + ','
                    });
                    this.setData({
                        authorizedPeople: authorizedPeople,
                        authorizedPeopleIds: authorizedPeopleIds
                    })
                }
            },
        }];
    },

    getChooseListColumns: function () {
        return [{index: true},{
            field: 'fileType',
            title: 'T_LIMS_KNOWLEDGELIBRARY.FILETYPE',
            type: 'select',
            category: 'sys',
            param: {
                codeCategoryId: 'knowledgelibrary-fileType'
            }
        }, {
            field: 'fileName',
            title: 'T_LIMS_KNOWLEDGELIBRARY.FILENAME'
        }, {
            field: 'fileSize',
            title: 'T_LIMS_KNOWLEDGELIBRARY.FILESIZE'
        }, {
            field: 'type',
            title: 'T_LIMS_KNOWLEDGELIBRARY.TYPE',
            type: 'select',
            category: 'sys',
            param: {
                codeCategoryId: 'knowledgelibrary-type'
            }
        }, {
            field: 'authorizedPeople',
            title: 'T_LIMS_KNOWLEDGELIBRARY.AUTHORIZEDPEOPLE'
        }, {
            field: 'activeStatus',
            title: 'T_LIMS_KNOWLEDGELIBRARY.ACTIVESTATUS',
            type: 'select',
            category: 'sys',
            param: {
                codeCategoryId: 'activatedFlag'
            }
        }];
    },

    baseUrl: IFM_CONTEXT + '/secure/baseline/basedata/knowledgelibrarys',

    // 以下为备忘录相关结构和地址

    getRemarkNoteEditListColumns: function () {
        return [{
            checkbox: true
        },{index: true}, {
            field: 'remarkNote',
            title: 'T_LIMS_REMARK_NOTE.REMARKNOTE',
            width: 1000,
            type: 'richText',
            validators : ['notEmpty']
        }, {
            field: 'createdByName',
            title: 'T_LIMS_REMARK_NOTE.CREATEDBYNAME',
            width : 120
        }, {
            field: 'createdTime',
            title: 'T_LIMS_REMARK_NOTE.CREATEDTIME',
            width : 160,
            type: 'dateTime',
        }];
    },

    getRemarkNoteBaseInfoFormFields: function () {
        return [{
            field: 'remarkNote',
            title: 'T_LIMS_REMARK_NOTE.REMARKNOTE',
            validators: ['strLength[0,100]'],
            type: 'textarea',
            colspan: 2,
            height: 160,
            preInsert: true
        }];
    },

    baseRemarkNoteUrl: IFM_CONTEXT + '/secure/baseline/basedata/remark-notes',

    //以下为实验信息表相关结构和地址

    getExperimentInfoEditListColumns: function () {
        return [{
            checkbox: true
        },{index: true}, {
            field: 'picture',
            title: 'T_LIMS_EXPERIMENT_INFORMATION.PICTURE',
            type: 'image',
            editor: true,
            onClick: function () {
                limsKnowledgelibrary.uploadPicture()
            }
        }, {
            field: 'title',
            title: 'T_LIMS_EXPERIMENT_INFORMATION.TITLE',
            editor: true,
            validators : ['notEmpty']
        }, {
            field: 'description',
            title: 'T_LIMS_EXPERIMENT_INFORMATION.DESCRIPTION',
            editor: true,
            validators : ['notEmpty']
        }];
    },

    uploadPicture: function () {
        var grid = Gikam.getComp('lims-experiment-information-edit-list-grid');
        var bizId = grid.getActivedRow().id;

        Gikam.create('simpleUploader', {
            id: 'experiment-information-picture-uploader',
            dbTable: 'T_LIMS_EXPERIMENT_INFORMATION',
            bizId: bizId,
            bizCategory: 'picture',
            accept: 'image',
            multiple: false,
            onAfterClose: function (fileList) {
                if (fileList.length === 0) {
                    return;
                }
                Gikam.getJson(Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}', {
                    id: fileList[0].id
                })).done(function (r) {
                    // 触发自动保存
                    grid.setData({
                        index: grid.getActivedRow().index,
                        id: bizId,
                        picture: r.downloadUrl,
                    })
                })
            }
        })
    },

    getExperimentInfoBaseInfoFormFields: function () {
        return [{
            field: 'picture',
            title: 'T_LIMS_EXPERIMENT_INFORMATION.PICTURE',
            validators: ['strLength[0,66]']
        }, {
            field: 'title',
            title: 'T_LIMS_EXPERIMENT_INFORMATION.TITLE',
            validators: ['strLength[0,66]'],
            preInsert: true
        }, {
            field: 'description',
            title: 'T_LIMS_EXPERIMENT_INFORMATION.DESCRIPTION',
            validators: ['strLength[0,66]'],
            type: 'textarea',
            height: 60,
            preInsert: true
        },];
    },

    baseExperimentInfoUrl: IFM_CONTEXT + '/secure/baseline/basedata/experiment-informations'
});
